/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JLabel;

public class GUI extends JFrame {
    // GUI - 2,5h x2 (2personer)
    // FIX DB CALLS - 2h x2 (2personer)
    
    private EventHandler eventHandler;
    
//    Login-Page
    private JLabel labelLogin;
    private JLabel labelPassword;
        
    private JTextField fieldLogin;
    private JPasswordField fieldPassword;
    
    private JButton confirmLogin;
    
//    Account-Page
    private JLabel textBalance;
    private JLabel textWithdraw;
    private JLabel textDeposit;
    
    private JTextField fieldWithdraw;
    private JTextField fieldDeposit;
    
    private JButton withdrawButton;
    private JButton depositButton;
    private JButton logOut;
    
    private int userID;
    
    public GUI () {
        setTitle("ATM MACHINE");
        setLayout(new FlowLayout());

        eventHandler = new EventHandler();
        
        labelLogin = new JLabel("Login: ", 10);
        add(labelLogin);
        
        fieldLogin = new JTextField(10);
        fieldLogin.setToolTipText("Enter your personal 10digit id here.");
        add(fieldLogin);
        
        labelPassword = new JLabel("Password: ", 10);
        add(labelPassword);
        
        fieldPassword = new JPasswordField(10);
        fieldLogin.setToolTipText("Enter your password here.");
        add(fieldPassword);
       
        confirmLogin = new JButton("Confirm Login");
        fieldLogin.setToolTipText("Click to login.");        
        add(confirmLogin);
        
        confirmLogin.addActionListener(eventHandler);
    }
    
    private void showLoginPage() {
        fieldLogin.setVisible(true);
        fieldPassword.setVisible(true);
        labelLogin.setVisible(true);
        labelPassword.setVisible(true);
        confirmLogin.setVisible(true);
        
        fieldLogin.setText("");
        fieldPassword.setText("");
    }
    
    private void hideLoginPage() {
        fieldLogin.setVisible(false);
        fieldPassword.setVisible(false);
        labelLogin.setVisible(false);
        labelPassword.setVisible(false);
        confirmLogin.setVisible(false);
        
    }
    
    private void createAccountPage() {
        textBalance = new JLabel("-1");
        add(textBalance);
        textBalance.setVisible(false);
        
        logOut = new JButton("Log out from your account");
        add(logOut);
        logOut.setVisible(false);
        
        depositButton = new JButton("Deposit");
        add(depositButton);
        depositButton.setVisible(false);
        
        withdrawButton = new JButton("Withdraw");
        add(withdrawButton);
        withdrawButton.setVisible(false);

        logOut.addActionListener(eventHandler);
        depositButton.addActionListener(eventHandler);
        withdrawButton.addActionListener(eventHandler);
    }    
    
    private void showAccountPage() {
        textBalance.setVisible(true);
        textBalance = db.getBalance(userID);
        logOut.setVisible(true);
        depositButton.setVisible(true);
        withdrawButton.setVisible(true);
    }
    
    private void hideAccountPage() {
        textBalance.setVisible(false);
        logOut.setVisible(false);    
        depositButton.setVisible(false);
        withdrawButton.setVisible(false);
        userID=-1;
        db.closeConnection();
               
    }
    
    private class EventHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            
            if(event.getSource()==confirmLogin) {
                userID = Integer.parseInt(fieldLogin.getText());
                String password = convertPassword(fieldPassword.getPassword());
                if (db.checkLogin(userID, password)) {
                    JOptionPane.showMessageDialog(null,"Correct");
                    hideLoginPage();
                    createAccountPage();
                    showAccountPage();
                } else {
                    JOptionPane.showMessageDialog(null,"Incorrect");
                }
            } else if(event.getSource()==logOut) {
                hideAccountPage();
                showLoginPage();
            } else if(event.getSource()==depositButton) {
                makeDeposit();
            } else if(event.getSource()==withdrawButton) {
                makeWithdraw();
            }
        }
    }
    
    private void makeDeposit() {
        String getInput = JOptionPane.showInputDialog("Enter Amount you want to deposit");
        float amount = db.getBalance(userID) - Float.parseFloat(getInput);
        db.setBalance(userID, amount);
        JOptionPane.showMessageDialog(null,"deposit didn't work");
        textBalance = db.getBalance(userID);
    }
    
    private void makeWithdraw() {
        String getInput = JOptionPane.showInputDialog("Enter Amount you want to withdraw");
        float amount = db.getBalance(userID) - Float.parseFloat(getInput);
        if (amount > 0) {
            db.setBalance(userID, amount);
            textBalance = db.getBalance(userID);
            JOptionPane.showMessageDialog(null,"Withdraw worked");         
        } else {
            JOptionPane.showMessageDialog(null,"Withdraw didn't work");
        }   
    }
    
    private String convertPassword(char[] oldPassword) {
        String newPassword = "";
        for (int i = 0;i < oldPassword.length; i++) {
            newPassword = newPassword + oldPassword[i];
        }
        return(newPassword);
    }           
}